
package net.atremmedia.gwt.actionjava.examples.sgi;

import actionjava.display.Bitmap;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import com.google.gwt.user.client.Timer;

public class ImgGallery extends Sprite
{
	private int count = 0;
	private Sprite imgGallery;
	private Shape imgGalleryMask;
	private Bitmap bitmapGallery1;
	private ImgGalleryItem[] items;
	private ImgGalleryItemVO[] imgGalleryItems;
	private Timer galleryTimer;
	private ImgGalleryDim imgGalleryDim;
	private Timer galleryTimer2;
	private Timer galleryTimer3;
	private Timer galleryTimer4;

	public ImgGallery(ImgGalleryItemVO[] imgGalleryItems)
	{
		this.imgGalleryItems = imgGalleryItems;
		initialize();
	}

	private void initialize()
	{
		// imgGalleryMask = new Shape();
		// Graphics graph = imgGalleryMask.getGraphics();
		// graph.beginStroke("#000");
		// graph.beginFill(Color.getRGBA(0xFF0000, 1));
		// try {
		// graph.decodePath("AmWPnQr7hJnpnMIwR25MBUXAAAIAAfOg");
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// graph.closePath();
		//
		// bitmapGallery1 = new Bitmap(new BitmapData("img/gallery2.png"));
		// bitmapGallery1.setX(-270);
		// bitmapGallery1.setY(-100);
		// bitmapGallery1.setMask(imgGalleryMask);
		//
		// addChild(bitmapGallery1);

		setX(730);
		setY(100);

		dim();

		items = new ImgGalleryItem[imgGalleryItems.length];
		for(int i = 0;i < imgGalleryItems.length;i++) {
			items[i] = new ImgGalleryItem(imgGalleryItems[i]);
			addChild(items[i]);
		}

		animate();
		// items[0].setAlpha(1);

		// swapChildren(items[0], items[items.length - 1]);
		// setChildIndex(getNumChildren() - 2, items[0]);
		// setChildIndex(getNumChildren(), imgGalleryDim);
		initTimer();
	}

	private void dim()
	{
		imgGalleryDim = new ImgGalleryDim();
		addChild(imgGalleryDim);
	}

	private void initTimer()
	{
		galleryTimer = new Timer()
		{
			@Override
			public void run()
			{
				animate();
			}
		};
		galleryTimer.scheduleRepeating(6000);
	}

	private void animate()
	{
		ImgGalleryItem oldItem = items[count];

		count++;
		if(count >= items.length) {
			count = 0;
		}

		ImgGalleryItem newItem = items[count];

		swapChildren(newItem, oldItem);

		imgGalleryDim.setColor(newItem.getColor());
		imgGalleryDim.setText(newItem.getTextX(), newItem.getTextY(), newItem.getText());
		imgGalleryDim.animateIn(newItem.getTextX(), newItem.getTextY());
		newItem.animateIn();
		animateOutTimer(oldItem);

		// animateDimTimer();
		// imgGalleryDim.animateIn();
		// animateInTimer(newItem);
		// animateOutTimer();
	}

	private void animateInTimer(final ImgGalleryItem item)
	{
		galleryTimer2 = new Timer()
		{
			@Override
			public void run()
			{
				item.animateIn();
			}
		};
		galleryTimer2.schedule(250);
		galleryTimer2.run();
	}

	private void animateOutTimer(final ImgGalleryItem item)
	{
		galleryTimer3 = new Timer()
		{
			@Override
			public void run()
			{
				item.animateOut();
			}
		};
		galleryTimer3.schedule(250);
		galleryTimer3.run();
	}

	private void animateDimTimer()
	{
		galleryTimer4 = new Timer()
		{
			@Override
			public void run()
			{
				// imgGalleryDim.animateIn();
			}
		};
		galleryTimer4.schedule(150);
		galleryTimer4.run();
	}
}
